package com.company.project.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.company.project.dao.VtManagerMapper;
import com.company.project.dao.VtZuzhuangMapper;
import com.company.project.dao.VtZzproductMapper;
import com.company.project.model.*;
import com.company.project.model.VO.*;
import com.company.project.service.VtZuzhuangService;
import com.company.project.core.AbstractService;
import org.apache.ibatis.jdbc.Null;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * Created by CodeGenerator on 2023/11/14.
 */
@Service
@Transactional
public class VtZuzhuangServiceImpl extends AbstractService<VtZuzhuang> implements VtZuzhuangService {
    @Resource
    private VtZuzhuangMapper vtZuzhuangMapper;
    @Resource
    private VtZzproductMapper vtZzproductMapper;

    @Resource
    private VtManagerMapper vtManagerMapper;
    @Override
    public List<ZuzhuangVO> findListByGroupId(VtZuzhuang vtZuzhuang) {
        List<VtZuzhuang> zhusuList = vtZuzhuangMapper.findListByGroupId(vtZuzhuang.getGroupid());
        List<ZuzhuangVO> zhusuVOS = new ArrayList<>();
        for (VtZuzhuang temp:zhusuList) {
            ZuzhuangVO zuzhuangVO = new ZuzhuangVO();
            zuzhuangVO.setOrderId(temp.getOrderId());
            VtZzproduct vtZzproduct = vtZzproductMapper.selectByPrimaryKey(Integer.parseInt(temp.getModel()));
            zuzhuangVO.setProductName(vtZzproduct.getName());
            List<String> opratorNameList = new ArrayList<>();
            for (Object id: JSONObject.parseArray(temp.getOperator())) {
                VtManager vtManager = vtManagerMapper.selectByPrimaryKey(id);
                opratorNameList.add(vtManager.getTruename());
            }
            zuzhuangVO.setOperatorName(String.join(",",opratorNameList));
            zuzhuangVO.setId(temp.getId());
            zuzhuangVO.setIsexamine(temp.getIsexamine());
            zuzhuangVO.setGroupZ(temp.getGroupZ());
            zhusuVOS.add(zuzhuangVO);
        }
        return zhusuVOS;
    }

    @Override
    public Integer getAccumulateNumber(VtZuzhuang vtZuzhuang) {
        VtZuzhuang zuzhuang = new VtZuzhuang();
        zuzhuang.setOrderId(vtZuzhuang.getOrderId());
        zuzhuang.setModel(vtZuzhuang.getModel());
        zuzhuang.setIsdel(0);
        List<VtZuzhuang> zhusuList = vtZuzhuangMapper.select(zuzhuang);
        Integer count = 0;

        return count;
    }

    @Override
    public List<ZuzhuangVO> findTodayListByGroupId(long startTime, long endTime, Integer groupId, Integer isExamine) {
        List<VtZuzhuang> zhusuList = vtZuzhuangMapper.findTodayListByGroupId(startTime,endTime,groupId,isExamine);
        List<ZuzhuangVO> zhusuVOS = new ArrayList<>();
        for (VtZuzhuang temp:zhusuList) {
            ZuzhuangVO zhusuVO = new ZuzhuangVO();
            zhusuVO.setOrderId(temp.getOrderId());
            VtZzproduct vtProduct = vtZzproductMapper.selectByPrimaryKey(temp.getModel());
            zhusuVO.setProductName(vtProduct.getName());
            List<String> opratorNameList = new ArrayList<>();
            for (Object id: JSONObject.parseArray(temp.getOperator())) {
                VtManager vtManager = vtManagerMapper.selectByPrimaryKey(id);
                opratorNameList.add(vtManager.getTruename());
            }
            zhusuVO.setOperatorName(String.join(",",opratorNameList));
            zhusuVO.setId(temp.getId());
            zhusuVO.setIsexamine(temp.getIsexamine());
            zhusuVOS.add(zhusuVO);
        }
        return zhusuVOS;
    }

    @Override
    public List<ZuzhuangVO> findHistoryListByGroupId(long startTime, Integer groupId, Integer isExamine) {
        List<VtZuzhuang> zhusuList = vtZuzhuangMapper.findHistoryListByGroupId(startTime,groupId,isExamine);
        List<ZuzhuangVO> zhusuVOS = new ArrayList<>();
        for (VtZuzhuang temp:zhusuList) {
            ZuzhuangVO zhusuVO = new ZuzhuangVO();
            zhusuVO.setOrderId(temp.getOrderId());
            VtZzproduct vtProduct = vtZzproductMapper.selectByPrimaryKey(temp.getModel());
            zhusuVO.setProductName(vtProduct.getName());
            List<String> opratorNameList = new ArrayList<>();
            for (Object id: JSONObject.parseArray(temp.getOperator())) {
                VtManager vtManager = vtManagerMapper.selectByPrimaryKey(id);
                opratorNameList.add(vtManager.getTruename());
            }
            zhusuVO.setOperatorName(String.join(",",opratorNameList));
            zhusuVO.setId(temp.getId());
            zhusuVO.setIsexamine(temp.getIsexamine());
            zhusuVOS.add(zhusuVO);
        }
        return zhusuVOS;
    }

    @Override
    public List<ZuzhuangVO> findTodayAllList(long startTime, long endTime, Integer isExamine) {
        List<VtZuzhuang> zhusuList = vtZuzhuangMapper.findTodayAllList(isExamine);
        List<ZuzhuangVO> zhusuVOS = new ArrayList<>();
        for (VtZuzhuang temp:zhusuList) {
            ZuzhuangVO zhusuVO = new ZuzhuangVO();
            zhusuVO.setOrderId(temp.getOrderId());
            zhusuVO.setGroupZ(temp.getGroupZ());
            VtZzproduct vtProduct = vtZzproductMapper.selectByPrimaryKey(Integer.parseInt(temp.getModel()));
            zhusuVO.setProductName(vtProduct.getName());
            List<String> opratorNameList = new ArrayList<>();
            for (Object id: JSONObject.parseArray(temp.getOperator())) {
                VtManager vtManager = vtManagerMapper.selectByPrimaryKey(id);
                opratorNameList.add(vtManager.getTruename());
            }
            zhusuVO.setOperatorName(String.join(",",opratorNameList));
            zhusuVO.setId(temp.getId());
            zhusuVO.setIsexamine(temp.getIsexamine());
            zhusuVOS.add(zhusuVO);
        }
        return zhusuVOS;
    }

    @Override
    public List<ZuzhuangVO> findHistoryAllList(Integer isExamine, FilterVO filterVO) {
        if (Objects.isNull(filterVO)) {
            filterVO = new FilterVO();
            filterVO.setIsExamine(isExamine);
        }else  {
            filterVO.setIsExamine(isExamine);
        }
        List<VtZuzhuang> zhusuList = vtZuzhuangMapper.findHistoryAllList(filterVO);
        List<ZuzhuangVO> zhusuVOS = new ArrayList<>();
        for (VtZuzhuang temp:zhusuList) {
            ZuzhuangVO zhusuVO = new ZuzhuangVO();
            zhusuVO.setGroupZ(temp.getGroupZ());
            zhusuVO.setOrderId(temp.getOrderId());
            VtZzproduct vtProduct = vtZzproductMapper.selectByPrimaryKey(Integer.parseInt(temp.getModel()));
            zhusuVO.setProductName(vtProduct.getName());
            List<String> opratorNameList = new ArrayList<>();
            for (Object id: JSONObject.parseArray(temp.getOperator())) {
                VtManager vtManager = vtManagerMapper.selectByPrimaryKey(id);
                opratorNameList.add(vtManager.getTruename());
            }
            zhusuVO.setOperatorName(String.join(",",opratorNameList));
            zhusuVO.setId(temp.getId());
            zhusuVO.setIsexamine(temp.getIsexamine());
            zhusuVOS.add(zhusuVO);
        }
        return zhusuVOS;
    }

    @Override
    public List<StatisticsListVO> getStatisticsList() {
        LocalDate currentDate = LocalDate.now();
        // 设置当天的开始时间
        LocalDateTime startOfDay = currentDate.atStartOfDay();
        long startTimestamp = startOfDay.toEpochSecond(ZoneOffset.UTC);
//        startTimestamp += 28800;
        // 设置当天的结束时间
        LocalTime endOfDay = LocalTime.MAX;
        LocalDateTime endOfDate = LocalDateTime.of(currentDate, endOfDay);
        long endTimestamp = endOfDate.toEpochSecond(ZoneOffset.UTC);
//        endTimestamp += 28800;
        // 输出结果
        System.out.println("当天的开始时间戳：" + startTimestamp);
        System.out.println("当天的结束时间戳：" + endTimestamp);
        Date day=new Date();
        SimpleDateFormat sdf= new SimpleDateFormat("MM-dd");
        System.out.println(sdf.format(day));
        List<StatisticsListVO> statisticsListVOList = new ArrayList<>();
        VtManager vtManager = new VtManager();
        vtManager.setRoleid(7);
        List<VtManager> vtManagerList = vtManagerMapper.select(vtManager);
        for (VtManager temp:vtManagerList ) {
            StatisticsListVO statisticsListVO = new StatisticsListVO();
            statisticsListVO.setTodayDate(sdf.format(day));
            statisticsListVO.setGroupId(temp.getGroupid());
            statisticsListVO.setClassesName("");
            statisticsListVO.setDirectorName(temp.getTruename());
            statisticsListVO.setOperatorCount(vtZuzhuangMapper.getOperatorCount(startTimestamp,endTimestamp,temp.getGroupid(), -1));
            if (Objects.isNull(statisticsListVO.getOperatorCount()) || statisticsListVO.getOperatorCount() == 0 ) continue;
            statisticsListVOList.add(statisticsListVO);
        }
        return statisticsListVOList;
    }

    public List<StatisticsDetailDO> getStatisticsDetail(Integer groupId, Integer classes) {
        LocalDate currentDate = LocalDate.now();
        // 设置当天的开始时间
        LocalDateTime startOfDay = currentDate.atStartOfDay();
        long startTimestamp = startOfDay.toEpochSecond(ZoneOffset.UTC);
        // 设置当天的结束时间
        LocalTime endOfDay = LocalTime.MAX;
        LocalDateTime endOfDate = LocalDateTime.of(currentDate, endOfDay);
        long endTimestamp = endOfDate.toEpochSecond(ZoneOffset.UTC);
        // 输出结果
        startTimestamp += 28800;
        endTimestamp += 28800;
        System.out.println("当天的开始时间戳：" + startTimestamp);
        System.out.println("当天的结束时间戳：" + endTimestamp);
        Date day = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        System.out.println(sdf.format(day));
        return vtZuzhuangMapper.getStatisticsDetail(startTimestamp,endTimestamp,groupId,classes);
    }
}
